#!/usr/bin/env python
# -*- coding: utf-8 -*-

#-*-
# UbiNote - A Lightweight Note-talking Application
#
# Copyright (C) 2006-2009 Shellex <5h3ll3x@gmail.com>
#
# UbiNote is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# UbiNote is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with UbiNote; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA



from dbmgr import dbmgr
from dbmap import block

class blockmgr:
    @staticmethod    
    def add(note_id, content, pos):
        c = dbmgr.cursor()
        c.execute('''INSERT INTO block 
            (note_id, content, pos) VALUES(?, ?, ?);''',
            (note_id, content, pos));
        dbmgr.commit();
        return c.lastrowid
    
    @staticmethod
    def remove(id):
        c = dbmgr.cursor()
        c.execute('''DELETE FROM block WHERE block.id=?''', (id,))
        dbmgr.commit()
        pass
    
    @staticmethod
    def search(keywords):
        pass
    
    @staticmethod
    def modify(id, content):
        c = dbmgr.cursor()
        c.execute('''UPDATE block SET content=? WHERE block.id=?''', (content, id))
        dbmgr.commit()
        return id 
    
    @staticmethod
    def count():
        return len(dbmgr.cursor().execute('select * from block').fetchall())
    
    @staticmethod
    def save_pos(ids):
        c = dbmgr.cursor()
        for i, id in enumerate(ids):
            c.execute('''UPDATE block SET pos=? WHERE block.id=?''', (i, id))
        dbmgr.commit()
    
    @staticmethod    
    def get_by_note_id(note_id):
        c = dbmgr.cursor()
        blocks = []
        raws = c.execute('select * from block where block.note_id = ?', (note_id, )).fetchall()
        for e in raws:
            b = block(e)
            blocks.append(b)
        return blocks
    
    @staticmethod
    def get(id):
        c = dbmgr.cursor()
        b = c.execute('''select * from block 
            where block.id=?''',
            (id,)).fetchone()
        return block(b)







